package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class PictureServiceImpl implements IPictureService {
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private AlbumMapper albumMapper;

    public PictureServiceImpl() {
        log.debug("创建业务对象：PictureServiceImpl");
    }

    @Override
    public void insert(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加图片】的业务，参数：{}", pictureAddNewDTO);
        Object queryResult = albumMapper.getStandardById(pictureAddNewDTO.getAlbumId());
        if (queryResult==null){
            String message = "添加图片失败，尝试添加的图片对应的相册不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO, picture);
        log.debug("即将执行插入数据");
        int rows = pictureMapper.insert(picture);
        if (rows != 1){
            String message = "添加图片失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除图片】的业务，参数id：{}", id );
        Object queryResult = pictureMapper.getStandardById(id);
        if (queryResult==null){
            String message = "删除图片失败，尝试删除的图片不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将执行删除数据！");
        int rows = pictureMapper.deleteById(id);
        if (rows != 1){
            String message = "删除图片失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }
}